package edu.chc.hhsh.ext.ext_search;

import java.util.ArrayList;
import java.util.Hashtable;

import com.pub.database.*;

public class Business {
   QueryBean gqbnDAO=null;

   public Business(){
   }

   void SetQueryBean(QueryBean pqbnDAO) throws Exception{
      gqbnDAO=pqbnDAO;
   }

   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view) throws Exception{
       if(view.getDataKind().equals("ext_list")){
           return getDataViewHtml(view.getDataView(),view.getDataViewForm(),view);
       }

       return "";
    }

    String getDataViewHtml(DataView dataView,DataViewForm DataViewForm,View view) throws Exception{
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(getDataViewPageAction(view));

        //設定要query的table list(以逗號隔開)
        DataViewForm.setTablesList(" school_ext ");

        //依欄位查詢值組合sql where
        DataViewForm.setSQLCondition(getDataViewWhere(view));

        //設定排序的欄位
        DataViewForm.setOrderByFields("school_ext.emp_name");

        //排序的方法(正序或倒序)
        DataViewForm.setOrderBySeq("asc");

        //group by 的欄位(以逗號隔開)
        DataViewForm.setGroupBy(null);

        //Having 的欄位(有設定group by時此欄位才有作用)
        DataViewForm.setHaving(null);

        //將欄位相關設定值設入DataViewForm
        DataViewForm.setColRelationUseArrayList(getDataViewColRelation(dataView,view));

        //設定DataView Header的<tr> html屬性
        DataViewForm.setTableHeaderHtml("class='td_head_grid' align='center'  width='680px'");

        DataViewForm.setJumpPageButtonShow(false);   //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(true);         //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(false);       //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false);           //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(false);         //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false);         //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false);          //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false);            //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false);          //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(true);  //當ResultSet無資料是否要秀Header
        DataViewForm.setAddNewShow(false);           //是否秀出新增鈕

        return dataView.getOutputDataView(DataViewForm);
   }

    /**
     * getDataViewColRelation
     *
     * @param dataView DataView
     * @param view View
     * @return ArrayList
     */
    private ArrayList getDataViewColRelation(DataView dataView, View view) {
        ArrayList laltColRelation = new ArrayList();
        Hashtable lhtColRelationDetail = new Hashtable();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "emp_id");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'工號'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='20%' align='center'");

        laltColRelation.add(new Hashtable(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "emp_dept");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'單位(處室)'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='20%' align='center'");

        laltColRelation.add(new Hashtable(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "emp_title");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'職稱'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='20%' align='center'");

        laltColRelation.add(new Hashtable(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "emp_name");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'姓名'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"width='20%' align='center'");

        laltColRelation.add(new Hashtable(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "emp_ext");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'分機'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "0");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"WIDTH='20%' align='center'");

        laltColRelation.add(new Hashtable(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        return laltColRelation;
    }

    /**
     * getDataViewPageAction
     *
     * @param view View
     * @return String
     */
    private String getDataViewPageAction(View view) {
        StringBuffer stbPageActionURI = new StringBuffer("ext_search.jsp?prgAction=&keyWords=");

        stbPageActionURI.append(view.getKeywords()==null ? "" : view.getKeywords());

        return stbPageActionURI.toString();
    }

    /**
     * getDataViewWhere
     *
     * @param view View
     * @return Object
     */
    private String getDataViewWhere(View view) throws Exception{
        StringBuffer stbCondition=new StringBuffer();

        if(view.getKeywords()!=null){
            if(stbCondition.length()!=0){//代表非where式的第一個欄位，需加 and
                stbCondition.append(" and ");
            }
            stbCondition.append(" school_ext.emp_dept like '%");
            stbCondition.append(view.getKeywords());
            stbCondition.append("%'");

            stbCondition.append(" or ");

            stbCondition.append(" school_ext.emp_title like '%");
            stbCondition.append(view.getKeywords());
            stbCondition.append("%'");

            stbCondition.append(" or ");

            stbCondition.append(" school_ext.emp_name like '%");
            stbCondition.append(view.getKeywords());
            stbCondition.append("%'");

            stbCondition.append(" or ");

            stbCondition.append(" school_ext.emp_ext like '%");
            stbCondition.append(view.getKeywords());
            stbCondition.append("%'");
        }

        return stbCondition.toString();
    }

    String actionForInit(View view) {
        return null;
    }

    String actionForEnd(View view) {
        return null;
    }

    void setButtonShow(View view, String prgAction, String prgStatus) {
        return;
    }

    void setReadonlyFields(View view, String prgAction, String prgStatus) {
        return;
    }

    void setShowFields(View view, String prgAction, String prgStatus) {
        return;
    }
}
